﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaobaoERP.Core.Common;
using TaobaoERP.BusinessTaobao.Common;
using System.Collections;

namespace TaobaoERP.BusinessTaobao.ParObject
{
    public class ParBase
    {
        //public ParBase(string _sip_apiname, string _sip_sessionid)
        //{
        //    sip_apiname = _sip_apiname;
        //    sip_sessionid = _sip_sessionid;
        //}
        /// <summary>
        /// ISV应用的appkey为软件互联平台分配给ISV应用的唯一编号。
        /// </summary>
        [Par(IsNeed = true)]
        public string sip_appkey = AppSettings.APP_ID;
        
        /// <summary>
        /// 服务请求时间戳(yyyy-mm-dd hh:mi:ss)。
        /// </summary>
        [Par(IsNeed = true)]
        public string sip_timestamp = System.DateTime.Now.ToString(Constants.SIP_TIMESTAMP_FORMATER);
        
        /// <summary>
        /// 在ISV应用中用户会话唯一标识。
        /// </summary>
        [Par(IsNeed = true)]
        public string sip_sessionid;
        /// <summary>
        /// 规定返回值的类型。支持两种格式json与xml。默认为xml数据格式。
        /// </summary>
        [Par(IsNeed = true)]
        public string sip_format="xml";
        /// <summary>
        /// 接口版本号
        /// </summary>
        [Par(IsNeed = true)]
        public string v="1.0";

        /// <summary>
        /// 获取key value 对应字符串
        /// </summary>
        /// <returns></returns>
        private SortedList GetParallelism()
        {
            SortedList mySL = new SortedList();
            
            Type type = this.GetType();
            //循环CmdObject的变量
            foreach (System.Reflection.FieldInfo fieldInfo in type.GetFields())
            {
                string tempFieldInfo = ReplaceTo(fieldInfo.Name);
                //循环CmdObject的变量属性
                
                    //判断是否为空
                if (fieldInfo.GetValue(this.MemberwiseClone()) != null)
                {
                    mySL.Add(tempFieldInfo, fieldInfo.GetValue(this.MemberwiseClone()).ToString());
                    //sql.AppendFormat("{0}={1}|", tempFieldInfo, fieldInfo.GetValue(this.MemberwiseClone()).ToString());
                }
                else
                {
                    foreach (Attribute at in fieldInfo.GetCustomAttributes(true))
                    {
                        if (fieldInfo.GetCustomAttributes(typeof(ParAttribute), false).Length > 0)
                        {
                            throw new InvalidOperationException(tempFieldInfo + "必须填写"); 
                        }
                    }
                }
                
            }
            return mySL;
        }
        /// <summary>
        /// 替换Dot 为.
        /// </summary>
        /// <param name="fieldinfo"></param>
        /// <returns></returns>
        private string ReplaceTo(string fieldinfo)
        {
            return fieldinfo.Replace("DOT", ".");
        }

        public override string ToString()
        {
            return Signature.DoSignature(GetParallelism());
        }
    }

    /// <summary>
    /// 参数属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Field)]
    public class ParAttribute : Attribute
    {
        private bool isNeed = false;
        public ParAttribute()
        {
            
        }
        /// <summary>
        /// 是否必须
        /// </summary>
        public bool IsNeed
        {
            get { return isNeed; }
            set { isNeed = value; }
        }
    }
}
